package com.cai.servlet.common.user;

import com.cai.dao.DbUtil;
import com.cai.dao.UserTopicDao;
import com.cai.dao.impl.UserTopicDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author RuoLi
 * 用户收藏题目请求以及用户题目正确率
 */
@WebServlet(name = "ServletGetTopicFavoritesStatus", value = "/common/GetTopicFavoritesStatus")
public class ServletGetTopicFavoritesStatus extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserTopicDao userTopicDao = new UserTopicDaoImpl();
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String userAccount = req.getParameter("userAccount");
        String detailedTopicId = req.getParameter("detailedTopicId");
        int status = userTopicDao.getThisTopicIsFavorites(Integer.parseInt(userAccount), Integer.parseInt(detailedTopicId));
        if (status == -1) {
            status = 0;
        }
        resp.getWriter().write(String.valueOf(status));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编辑题目收藏状态
        UserTopicDao userTopicDao = new UserTopicDaoImpl();
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        int userAccount = Integer.parseInt(req.getParameter("userAccount"));
        int detailedTopicId = Integer.parseInt(req.getParameter("detailedTopicId"));
        int favorites = Integer.parseInt(req.getParameter("favorites"));
        //检查用户是否有记录 没有执行添加
        int status = userTopicDao.updateTopicFavorites(userAccount, detailedTopicId, favorites);
        //返回收藏状态
        resp.getWriter().write(String.valueOf(status));
    }
}
